let websock = null;
let global_callback = null
let wsUrl = 'ws://192.168.0.152:8083/' 
let timer = null
let msgCmd = ''
//判断是否手动关闭
let manual = false
// socket
class sock {
    // 初始化socket
    initWebSocket(){
        websock = new WebSocket(wsUrl)
        websock.onopen = (e) => { 
            this.websocketOpen(e)
        }
        websock.onmessage = (e) => {
            this.websocketonmessage(e)
        }
        websock.onclose = (e) => {
            this.websocketclose(e)
        }
        // 链接发生错误的回调
        websock.onerror = (e) => {
            console.log("错误返回：",e)
        }
    }
    // 数据接收
    websocketonmessage(e){
        global_callback(e)
        // let _this = this
        // _fileReader(e.data,(data) => {
        //     let msg = _msgRsq(data)
        //     // 心跳验证
        //     if(msg.cmd === 101){
        //         if(msg.code === 0){
        //             var msgBody = msg.body
        //             var vcr = _verifyConnRsp(msgBody)
        //             _this.validationCompleted(vcr)
        //         }
        //     }
        //     // 发送心跳
        //     if(msg.cmd === 51){
        //         if(msg.code === 0){
        //             var pong = _connPong(msg.body)
        //             console.log(pong.uid)
        //         }
        //     }
        //     // 被踢下线
        //     if(msg.cmd === 53){
        //         msgCmd = msg.cmd
        //         store.state.user.token = ""
        //         store.state.user.uid = ""
        //         sessionStorage.clear()
        //         Message.error(msg.err_msg)
        //     }

        //     store.commit('RECEIVE_MSG',e)
        //     if(global_callback){
        //         global_callback(e)
        //     }
        // })
    }
    // 数据发送
    websocketsend(agentData){
        if(!manual){
            websock.send(agentData)
        }
    }
    // 关闭
    websocketclose(e){
        if(!manual){
            let _this = this
            clearInterval(timer) //关闭定时器
            setTimeout(function () {
                if(msgCmd != 53){
                    _this.initWebSocket()
                }
            },2000)
        }
    }
    // 手动关闭
    webClose(){
        manual = true
        websock.close()
    }
    // socket成功返回
    websocketOpen(e){
        console.log('socket连接成功')
        // manual = false
        // var connParm = {
        //     cmd: 100,
        //     seq: 1,
        //     body: _verifyConnReq()
        // }
        // this.websocketsend(_msgReq(connParm))
    }
    // send调用方法
    sendSock(agentData,callback){
        let _this = this
        global_callback = callback
        if (websock.readyState === websock.OPEN) {
            //若是sock开启状态
            _this.websocketsend(agentData)
        }else if (websock.readyState === websock.CONNECTING) {
            // 若是 正在开启状态，则等待1s后重新调用
            setTimeout(function () {
                _this.sendSock(agentData,callback)
            }, 1000);
        }else {
            // 若未开启 ，则等待1s后重新调用
            setTimeout(function () {
                _this.sendSock(agentData,callback)
            }, 1000)
        }
    }
    // 验证通过回调
    validationCompleted(vcr){
        // // ping pong
        // this.heartBeat(vcr.heartbeatFrequency)
        // // 情况chat
        // store.state.chat.mailList = []
        // store.state.chat.sessions = []
        // // 获取本人用户资料
        // store.dispatch("getUserInfo",[store.state.user.uid])
        // // 获取好友通讯录
        // store.dispatch('getMailList')
        // // 获取群组通讯录
        // store.dispatch('getJoinGroupInfo')
    }
    // ping pong
    heartBeat(num){
        let _this = this
        let second = num+'000'
        timer = setInterval(()=>{
            _this.websocketsend(_msgReq({cmd: 50,seq: 1,body: _connPing()}))
        },second)
    }
}

const socket = new sock()

export default socket